<?php 
/**
 *  callServer API
 */
namespace Client\Common;

class VerifyHelper
{
    public $cellphone = '';

    /**
     * verify Cellphone
     * @return boolean 
     */
    public function verifyCellphone(){
        if(preg_match("/^(13[0-9]|15[012356789]|17[0678]|18[0-9]|14[57])[0-9]{8}$/",$this->cellphone)) {
            return true;
        }else {
            return false;
        }
    }
    
    public function isLogin(){
        if (isset($_SESSION['user']['user_id']) && !empty($_SESSION['user']['user_id'])) {
            return $_SESSION['user']['user_id'];
        }else {
            return false;
        }
    }
    
    public static  function text($text, $parseBr = false, $nr = false){
        $text = htmlspecialchars_decode($text);
        $text = self::safe( $text, "text" );
        if (!$parseBr && $nr){
            $text = str_ireplace( array( "\r", "\n", "\t", "&nbsp;" ), "", $text );
            $text = htmlspecialchars( $text, ENT_QUOTES );
        }else if(!$nr){
            $text = htmlspecialchars( $text, ENT_QUOTES );
        }else{
            $text = htmlspecialchars( $text, ENT_QUOTES );
            $text = nl2br( $text );
        }
        $text = trim( $text );
        return $text;
    }
    
    public static function safe($text, $type = "html", $tagsMethod = true, $attrMethod = true, $xssAuto = 1, $tags = array( ), $attr = array( ), $tagsBlack = array( ), $attrBlack = array( )){
        $text_tags = "";
        $font_tags = "<i><b><u><s><em><strong><font><big><small><sup><sub><bdo><h1><h2><h3><h4><h5><h6>";
        $base_tags = $font_tags."<p><br><hr><a><img><map><area><pre><code><q><blockquote><acronym><cite><ins><del><center><strike>";
        $form_tags = $base_tags."<form><input><textarea><button><select><optgroup><option><label><fieldset><legend>";
        $html_tags = $base_tags."<ul><ol><li><dl><dd><dt><table><caption><td><th><tr><thead><tbody><tfoot><col><colgroup><div><span><object><embed>";
        $all_tags = $form_tags.$html_tags."<!DOCTYPE><html><head><title><body><base><basefont><script><noscript><applet><object><param><style><frame><frameset><noframes><iframe>";
        $text = strip_tags( $text, ${ $type."_tags" } );
        if($type != "all"){
            while ( preg_match( "/(<[^><]+) (onclick|onload|unload|onmouseover|onmouseup|onmouseout|onmousedown|onkeydown|onkeypress|onkeyup|onblur|onchange|onfocus|action|background|codebase|dynsrc|lowsrc)([^><]*)/i", $text, $mat ) )
            {
                $text = str_ireplace( $mat[0], $mat[1].$mat[3], $text );
            }
            while ( preg_match( "/(<[^><]+)(window\\.|javascript:|js:|about:|file:|document\\.|vbs:|cookie)([^><]*)/i", $text, $mat ) )
            {
                $text = str_ireplace( $mat[0], $mat[1].$mat[3], $text );
            }
        }
        return $text;
    }
    
    
    //获取客户端IP地址
    public function getClientIp(){
        $ip = $_SERVER['REMOTE_ADDR'];
        if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP']))
        {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
        elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches))
        {
            foreach ($matches[0] AS $xip)
            {
                if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip))
                {
                    $ip = $xip;
                    break;
                }
            }
        }
        return $ip;
    }
    
    /**
     * 暂不使用  
     * SERVER_PORT Web 服务器使用的端口。默认值为 “80”。如果使用 SSL 安全连接，则这个值为用户设置的 HTTP 端口。
     * @return boolean
     */
    public function isHttps(){
        if(!empty($_SERVER['HTTPS']))  return 'https';
        if($_SERVER['HTTPS'] === 1){  //Apache
            return 'https';
        }elseif($_SERVER['HTTPS'] === 'on'){ //IIS
            return 'https';
        }elseif($_SERVER['SERVER_PORT'] == 443){ //其他
            return 'https';
        }
        return 'http';
    }
   

}